Mike Soft Labs
·Follow
4 min read·Sep 11, 2023--
In today’s fast-paced development environment, containerization has become a crucial tool for deploying and managing applications efficiently. Docker, with its ability to encapsulate applications and their dependencies in containers, is a popular choice for many developers. One common use case is setting up a LAMP (Linux, Apache, MySQL, PHP) server, a foundation for countless web applications. In this article, we will walk you through the process of setting up a LAMP server using Docker images, simplifying deployment and ensuring a consistent environment for your web projects.
PrerequisitesBefore diving into the tutorial, make sure you have the following prerequisites in place:
Docker: Ensure that Docker is installed on your system. If not, follow the official installation guide for your operating system: Docker Installation Guide.Docker Compose: Install Docker Compose if it’s not already on your system. Docker Compose is a tool for defining and running multi-container Docker applications. You can find installation instructions here: Docker Compose Installation.Step 1: Create a Project DirectoryStart by creating a dedicated directory for your LAMP server project. Open your terminal and run the following command:
mkdir lamp-docker && cd lamp-dockerStep 2: Create a Docker Compose FileNext, create a Docker Compose file named docker-compose.yml in your project directory. This file defines the services, networks, and volumes required for your LAMP server setup.
version: '3.8'#check if there is a new docker compose versionservices: web:image: php:8.2-apache#check the php version you need for your projectports: - "80:80"#this line maps your pc port to the container portdepends_on: - db#this line links this container to the db containervolumes: - ./html:/var/www/html#this line maps the content of ./html in your pc to the /var/www/html of the container db:image: mysql:8.1.0#check the mysql version you need for your projectenvironment: MYSQL_ROOT_PASSWORD: root_password#you can change the mysql root password here MYSQL_DATABASE: lamp_db#you can change the database name herevolumes: - ./mysql_data:/var/lib/mysql#this line maps the content of ./mysql_data in your pc to the /var/lib/mysql of the container phpmyadmin:image: phpmyadmin/phpmyadminports: - "8080:80"#this line maps your pc port to the container portdepends_on: - db#this line links this container to the db containerenvironment: PMA_HOST: dbIn this configuration, we have three services:
web: This service uses the php:8.2-apache image to run the Apache web server and PHP. It exposes port 80 for HTTP traffic and mounts the ./html directory as the web root.db: This service uses the mysql:8.1.0 image to run the MySQL database server. It sets environment variables for the root password and the database name and mounts the ./mysql_data directory for persistent data storage.phpmyadmin: This service uses the phpmyadmin/phpmyadmin image to run PHPMyAdmin, a web-based database management tool, and exposes it on port 8080. It connects to the db service for MySQL access.Step 3: Create HTML FilesCreate an html directory in your project folder and place your web application files or HTML documents inside it.
mkdir htmlStep 4: Build and Run the ContainersNow that you have your Docker Compose file and web files in place, it’s time to build and run the containers. Run the following command in your project directory:
docker-compose up -dThis command tells Docker Compose to start the services defined in the docker-compose.yml file in detached mode (in the background).
Step 5: Access Your LAMP ServerOnce the containers are up and running, you can access your LAMP server by opening a web browser and navigating to http://localhost. You should see your web application or HTML files served by the Apache web server.
You can also access PHPMyAdmin by navigating to http://localhost:8080. Use the MySQL root credentials you defined in the docker-compose.yml file to log in.
ConclusionIn this tutorial, we’ve walked through the process of setting up a LAMP server using Docker images and Docker Compose. This approach provides a clean and reproducible development environment for your web projects and simplifies the deployment process. With Docker, you can easily manage your LAMP stack, ensuring consistency across different development and production environments.
Remember that this is just the beginning of what you can do with Docker and containerization. As you become more comfortable with these technologies, you can explore additional features and optimizations to further enhance your development and deployment workflows.
If you found this helpful, please give the article a clap and star my GitHub repository. It’s free for you, but it greatly supports me in continuing to create high-quality contents!
And if you’d like to further show your support, feel free to buy me a coffee😊 buymeacoffee.com/mikedeejoi
If you’d like professional help building custom bots or automation solutions, feel free to contact me via my Fiverr profile: [Mike Soft Labs]. I’d be happy to help you take your projects to the next level!
Let me know in the comments if you want to deeply dive into specific aspects or features!
Happy coding! If you found this tutorial helpful, feel free to share it or drop a comment below!